<?php

/**
 * Created by PhpStorm.
 * User: li
 * Date: 15-11-19
 * Time: 下午9:47
 */
class MY_Admin_Controller extends MY_View_Controller
{
    /**
     * 分站管理员
     *
     * @var array
     */
    protected $site_admin;

    /**
     * @var Tank_auth
     */
    public $tank_auth;

    /**
     * @var resource_acl_model
     */
    public $resource_acl_model;

    function __construct(){
        parent::__construct();
        //解析站点信息
        $this->load->helper('file_helper');
        $this->load->helper('my_helper');
        $this->assign('static', '/static/m-admin/');
        $this->assign('views_path', $this->get_views_path());
        $this->load->library('session');
        $this->load->library('Auth_nuan');
        $this->_login_user = @$_SESSION['login_user'];
        $this->auth();
    }

    protected function auth()
    {
        $this->site_admin = $this->session->userdata('site_admin');
        if ($this->controller != 'login') {
            if (!$this->site_admin) {
                redirect('m-admin/login');
            }
            $this->load->model('Site_roles');
            $mapper = include APPPATH.'modules/m-admin/resource_mapper.php';
            if ($this->site_admin['roles'] == 'SUPERADMIN') {
                $channels = $mapper['channels'];
                $menus = $mapper['menus'];
            }else{
                if (isset($this->site_admin['admin_channels']) && isset($this->site_admin['admin_menus'])) {
                    $channels = $this->site_admin['admin_channels'];
                    $menus = $this->site_admin['admin_menus'];
                } else {
                    $channels = $mapper['channels'];

                    $menus = [];

                    foreach ($mapper['menus'] as $channel_key => $sub1_menus) {
                        $menus[$channel_key] = [];
                        if ($sub1_menus) {
                            foreach ($sub1_menus as $sub1_menu_key => $sub2_menus) {
                                foreach ($sub2_menus as $sub2_menu_name => $sub2_menu) {
                                    if (in_array($sub2_menu['url'], $this->site_admin['menus'])) {
                                        $menus[$channel_key][$sub1_menu_key][$sub2_menu_name] = [
                                            'url' => $sub2_menu['url'],
                                        ];
                                    }
                                }
                            }
                        }

                        if (count($menus[$channel_key]) == 0) {
                            unset($channels[$channel_key]);
                            unset($menus[$channel_key]);
                        }
                    }
                    $this->site_admin['admin_channels'] = $channels;
                    $this->site_admin['admin_menus'] = $menus;

                    $this->session->set_userdata('site_admin', $this->site_admin);
                }
                $resource = $this->module .'/';
                if ($this->directory) {
                    $resource .= $this->directory.'/';
                }

                $resource .= $this->controller;

                $this->load->model('resource_acl_model');

                if (!$this->resource_acl_model->is_access($resource, $this->action, $this->site_admin['roles'])) {
                    show_error('没有访问权限', 403);
                }
            }
            $this->assign('channels', $channels);
            $this->assign('menus', $menus);
            $this->assign('index_channel','system');
            //var_dump($menus);
            $this->assign('site_admin', $this->site_admin);
        }
    }
}